package com.examples.oryarok;

import static com.examples.oryarok.Constants.*;
import static com.examples.oryarok.HelloTabWidget.dayTemp;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class WeeklyList extends Activity {
	private MeetDataDbHelper meetEntry;
	
	//private static String[] FROM = {_ID,DATE,DAY,TIME,NAME, };
	private static String ORDER_BY = NAME + " DESC";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_weekly);
        
        dayTemp = "30.08.2011";//TODO remove this
	}
	@Override
	public void onResume(){
		super.onResume();
        meetEntry = new MeetDataDbHelper(this);
        try {
        	addEntry("Denis Surkes");
        	Cursor cursor = getEntry();
        	showEntry(cursor);
        }
        finally {
        	meetEntry.close();
        }
    }
	
	private void addEntry(String tempStr){
		SQLiteDatabase db = meetEntry.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DATE, "30.08.2011");
		values.put(DAY, "Tue");
		values.put(TIME, "19:00");
		values.put(NAME, tempStr);
		values.put(GENDER, "M");
		values.put(PHONE1, "0547590552");
		values.put(ADDRESS, "1151/25");
		values.put(STATUS, "ishur");
		values.put(TYPE, "miphgash");
		values.put(NUM_CONTACTS, 2);
		values.put(NOTE, "bla bla");
		db.insertOrThrow(TABLE_NAME, null, values);
		
	}
	
	private Cursor getEntry(){
		//page 187 on pdf
		SQLiteDatabase db = meetEntry.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
		startManagingCursor(cursor);
		return cursor;
	}
	
	private void showEntry(Cursor cursor) {
		// Stuff them all into a big string
		StringBuilder builder = new StringBuilder("Meetings List:\n" );
		while (cursor.moveToNext()) {
			// Could use getColumnIndexOrThrow() to get indexes
			long id = cursor.getLong(0);
			String date = cursor.getString(1);
			String day = cursor.getString(2);
			String time = cursor.getString(3);
			String name = cursor.getString(4);
			builder.append(id).append(": ");
			builder.append(date).append(" ");
			builder.append(day).append(", ");
			builder.append(time).append(" - ");
			builder.append(name).append("\n");
		}
		// Display on the screen
		TextView text = (TextView) findViewById(R.id.list_weekly_text);
		text.setText(builder);
	}

}